package com.base.myproject.client.development;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseCheckGroup;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;

/**
 * @author cxghiu4102@gmail.com 2010-11-24 问题跟踪
 * 
 */
public class Question extends BaseJDBCPanel {
	PagingToolBar toolBar1 = null;
	private Button search = new Button("查询");
	
	public Question() {
		init();
	}

	public void init() {
		search.addListener(Events.OnClick, getListener());
		initwidget();
		registerMainStore(store);
	}

	private void initwidget() {
		toolBar1 = new PagingToolBar(10);
		
		VerticalPanel vp = new VerticalPanel();
		vp.setSpacing(5);
		
		FieldSet fs2 = new FieldSet();
		fs2.add(createSearchForm());
		fs2.setHeading("问题查询");
		HorizontalPanel hp = new HorizontalPanel();
		hp.add(search);
		fs2.add(hp);
		vp.add(fs2);
		
		ContentPanel panel = new ContentPanel();
		panel.setHeaderVisible(false);
		panel.add(getBaseGrid());
		panel.setBottomComponent(toolBar1);
		vp.add(panel);
		
		FieldSet fs1 = new FieldSet();
		fs1.add(createForm());
		fs1.setHeading("问题跟踪");
		vp.add(fs1);
		add(vp);
	}

	BaseStore<BusiObject> store = null;

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("status");
		type.getField("status").setType(String.class);
		type.addField("degree");
		type.getField("degree").setType(String.class);
		type.addField("question");
		type.getField("question").setType(String.class);
		type.addField("answer");
		type.getField("answer").setType(String.class);
		type.addField("questionPeople");
		type.getField("questionPeople").setType(String.class);
		type.addField("answerPeople");
		type.getField("answerPeople").setType(String.class);
		type.addField("questionDate");
		type.getField("questionDate").setType(Date.class);
		type.addField("answerDate");
		type.getField("answerDate").setType(Date.class);
		type.addField("type");
		type.getField("type").setType(Integer.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("dp_question");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select * from dp_question order by questionDate desc");
		store = new BaseStore<BusiObject>(type, info);
		store.setToolBar(toolBar1);
		return store;
	}
	
	FormPanel searchForm = null;
	BaseComboBoxX bx1;
	BaseComboBoxX bx2;
	TextField<String> text1;
	TextField<String> text2;
	DateField date1;
	DateField date2;
	DateField date3;
	DateField date4;
	
	private FormPanel createSearchForm() {
		searchForm = new FormPanel();
		
		TableLayout tl = new TableLayout(8);
		searchForm.setBodyBorder(false);
		searchForm.setPadding(4);

		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(2);
		searchForm.setLayout(tl);
		searchForm.setHeaderVisible(false);
		
		searchForm.add(new Label("完成状态："), td);
		bx1 = new BaseComboBoxX("name", "dictionary", "where type='status'");
		searchForm.add(bx1);
		
		searchForm.add(new Label("紧急程度："), td);
		bx2 = new BaseComboBoxX("name", "dictionary", "where type='jjcl'");
		searchForm.add(bx2);
		
		searchForm.add(new Label("提交人："), td);
		text1 = new TextField<String>();
		searchForm.add(text1, td);
		
		searchForm.add(new Label("处理人："), td);
		text2 = new TextField<String>();
		searchForm.add(text2, td);
		
		searchForm.add(new Label("提交日期从："), td);
		date3 = new DateField();
		date3.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		searchForm.add(date3);
		
		searchForm.add(new Label("到："), td);
		date4 = new DateField();
		date4.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		searchForm.add(date4);
		
		searchForm.add(new Label("解决日期从："), td);
		date1 = new DateField();
		date1.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		searchForm.add(date1);
		
		searchForm.add(new Label("到："), td);
		date2 = new DateField();
		date2.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		searchForm.add(date2);
		return searchForm;
	}

	BaseFormPanel form = null;

	BaseFormPanel createForm() {
		BaseFormPanel form = new BaseFormPanel(getStore()); // false设置让form不会提示用户新
		// 增加东西，由grid控制增加
		TableLayout tl = new TableLayout(8);
		form.setBodyBorder(false);
		form.setPadding(4);

		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(2);

		TableData td2 = new TableData(); // 定义一个table用来装area
		td2.setRowspan(1);
		td2.setHorizontalAlign(HorizontalAlignment.LEFT);
		td2.setVerticalAlign(VerticalAlignment.TOP);
		td2.setPadding(2);
		td2.setColspan(2); // 定义每个area占3列的位置
		
/*		TableData td3 = new TableData(); // 定义一个table用来装area
		td3.setRowspan(1);
		td3.setHorizontalAlign(HorizontalAlignment.LEFT);
		td3.setVerticalAlign(VerticalAlignment.TOP);
		td3.setPadding(2);
		td3.setColspan(7); // 定义每个area占3列的位置
*/
		form.setLayout(tl);
		form.setHeaderVisible(false);
		
		form.add(new Label("类别："), td);
		String[] name = new String[]{"界面", "新增", "变更", "修补", "优化", "底层", "BUG"};
		BaseCheckGroup checkGroup = new BaseCheckGroup(name);
		checkGroup.setName("type");
		form.add(checkGroup, td);

		form.add(new Label("问题综述:"), td);
		TextArea C_num7 = new TextArea();
		C_num7.setWidth(325);
		C_num7.setHeight(150);
		C_num7.setName("question");
		form.add(C_num7, td2);
		
		form.add(new Label("解决办法:"), td);
		TextArea C_num8 = new TextArea();
		C_num8.setWidth(325);
		C_num8.setHeight(150);
		C_num8.setName("answer");
		form.add(C_num8, td2);
		form.inited();
		return form;
	}

	// 创建一个grid
	BaseGrid<BusiObject> grid = null;

	BaseGrid<BusiObject> getBaseGrid() {
		if (grid == null) {
			createGrid();
		}

		return grid;
	}

	void createGrid() {
		CheckBoxSelectionModel<BusiObject> cbs = new CheckBoxSelectionModel<BusiObject>();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

		ColumnConfig C2 = new ColumnConfig("status", "完成状态", 80);
		BaseComboBoxX bx1 = new BaseComboBoxX("name", "dictionary", "where type='status'");
		C2.setEditor(bx1.getCellEditor());
		columns.add(C2);
		ColumnConfig C3 = new ColumnConfig("degree", "紧急程度", 80);
		BaseComboBoxX bx2 = new BaseComboBoxX("name", "dictionary", "where type='jjcl'");
		C3.setEditor(bx2.getCellEditor());
		columns.add(C3);
		ColumnConfig C6 = new ColumnConfig("questionPeople", "提交人", 80);
		TextField<String> text1 = new TextField<String>();
		C6.setEditor(new CellEditor(text1));
		columns.add(C6);
		ColumnConfig C7 = new ColumnConfig("answerPeople", "处理人", 80);
		TextField<String> text2 = new TextField<String>();
		C7.setEditor(new CellEditor(text2));
		columns.add(C7);
		ColumnConfig C8 = new ColumnConfig("questionDate", "提交日期", 120);
		C8.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		DateField date1 = new DateField();
		date1.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		C8.setEditor(new CellEditor(date1));
		columns.add(C8);
		ColumnConfig C9 = new ColumnConfig("answerDate", "解决日期", 120);
		C9.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		DateField date2 = new DateField();
		date2.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		C9.setEditor(new CellEditor(date2));
		columns.add(C9);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid<BusiObject>(getStore(), cm);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid.setSelectionModel(cbs);
		grid.setLoadMask(true);
		grid.setBorders(false);
		grid.setSize(910, 200);
	}
	
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				String sql = "select * from dp_question where 1=1";
				String where = "";
				if (!bx1.getRawValue().equals("")) {
					where = where + " and status = '" + bx1.getRawValue() + "'";
				}
				if (!bx2.getRawValue().equals("")) {
					where = where + " and degree = '" + bx2.getRawValue() + "'";
				}
				if (!text1.getRawValue().equals("")) {
					where = where + " and questionPeople = '" + text1.getRawValue() + "'";
				}
				if (!text2.getRawValue().equals("")) {
					where = where + " and answerPeople = '" + text2.getRawValue() + "'";
				}
				if (!date1.getRawValue().equals("")) {
					where = where + " and answerDate >= '" + date1.getRawValue() + "'";
				}
				if (!date2.getRawValue().equals("")) {
					where = where + " and answerDate <= '" + date2.getRawValue() + "'";
				}
				if (!date3.getRawValue().equals("")) {
					where = where + " and questionDate >= '" + date3.getRawValue() + "'";
				}
				if (!date4.getRawValue().equals("")) {
					where = where + " and questionDate <= '" + date4.getRawValue() + "'";
				}
				
				sql = sql + where + " order by questionDate desc";
				grid.getStore().getStoreinfo().setSql(sql);
				grid.getStore().reload();
			}
		};
		return defaultlistener;
	}
}
